#include "common.h"

#define ADC_Baseline 3300  // ADC的输入电压基准 3300mV
#define ADC_Accuracy 4095  // 12位ADC  4096

volatile unsigned int adc_app_buf[ADC_DMA_BUF_NUM][ADC_CHANNEL_NUM];

#if 0
const unsigned short NTC[100]=  //0~89度热敏电阻值  单位Ohm
{
	33620,31893,30266,28732,27287,25924,24638,23425,22279,21197,    //0~9
	20174,19208,18294,17429,16611,15837,15103,14409,13750,13127,    //10~19
	12535,11974,11441,10935,10455,10000,9566,9155,8763,8391,        //20~29
	8037,7700,7379,7073,6782,6505,6241,5989,5749,5520,              //30~39
	5301,5092,4893,4703,4521,4348,4182,4023,3871,3726,              //40~49
	3588,3455,3328,3206,3090,2978,2871,2768,2670,2576,              //50~59
	2486,2399,2316,2236,2159,2086,2015,1947,1882,1820, 	            //60~69
	1759,1701,1646,1592,1541,1491,1443,1398,1353,1311,              //70~79
	1270,1230,1192,1156,1120,1086,1053,1022,991,962,                //80~89
	933,906,879,853,829,805,782,760,738,717
};
#endif

#define NTC_TABLE_LEN (sizeof(NTC) / 4)

const unsigned int NTC[] = {
    // 0~89度热敏电阻值  单位Ohm
    286228, 268577, 252132, 236805, 222511, 209176, 196727, 185102, 174239, 164085,  //-40~-31
    154588, 145702, 137384, 129595, 122296, 115455, 109040, 103021, 97372,  92068,   //-30~-21
    87086,  82493,  78166,  74089,  70246,  66622,  63204,  59979,  56935,  54061,   //-20~-11
    51347,  48738,  46360,  44069,  41902,  39853,  37915,  36080,  34342,  32697,   //-10~-1
    31139,  29655,  28250,  26919,  25657,  24461,  23326,  22250,  21229,  20259,   // 0~9
    19339,  18465,  17634,  16845,  16096,  15384,  14705,  14060,  13446,  12863,   // 10~19
    12307,  11778,  11274,  10793,  10336,  9900,   9476,   9072,   8688,   8321,    // 20~29
    7971,   7638,   7320,   7017,   6727,   6451,   6187,   5935,   5695,   5465,    // 30~39
    5246,   5036,   4835,   4644,   4461,   4285,   4118,   3957,   3804,   3657,    // 40~49
    3516,   3383,   3255,   3133,   3016,   2904,   2796,   2693,   2595,   2500,   2409, 2322, 2239, 2159, 2082, 2008, 1937, 1869, 1804, 1741, 1681,
    1623,   1567,   1513,   1462,   1412,   1365,   1319,   1275,   1233,   1192,   1153, 1115, 1079, 1044, 1011, 978,  947,  917,  888,  // 80~89
    861,    834,    808,    783,    759,    735,    713,    691,    670,    650,                                                          // 90~99
    630,    612,    593,    576,    559,    543                                                                                           // 100~105
};

/* 线性插值计算NTC温度，单位0.1℃*/
short LinearInsert(unsigned int T_Value)
{
    unsigned char i;
    unsigned char Hptr      = NTC_TABLE_LEN - 1;  // 高端变量
    unsigned char Lptr      = 0;                  // 低端变量
    unsigned char ptr       = 0;                  // 查数变量
    unsigned int  buffer1   = 0;
    unsigned int  buffer2   = 0;
    float         F_Value   = 0;
    short         Tem_Value = 0;
    if (T_Value >= NTC[0])  // 大于0度时的电阻
    {
        Tem_Value = -400;
        return (Tem_Value);
    } else if (T_Value <= NTC[NTC_TABLE_LEN - 1])  // 大于89度时的电阻
    {
        Tem_Value = 1050;
        return (Tem_Value);
    } else {
        for (i = 0; i < 8; i++) {
            ptr = (Hptr + Lptr) / 2;
            if (T_Value > NTC[ptr]) {
                if (T_Value <= NTC[ptr - 1]) {
                    ptr = ptr - 1;
                    break;
                } else {
                    Lptr = Lptr;  // 下限不变
                    Hptr = ptr;   // 上限变成当前值
                }
            } else if (T_Value < NTC[ptr]) {
                if (T_Value >= NTC[ptr + 1]) {
                    ptr = ptr;
                    break;
                } else {
                    Lptr = ptr;   // 下限变为当前值
                    Hptr = Hptr;  // 上限不变
                }
            } else {
                break;
            }
        }
        buffer1   = NTC[ptr] - NTC[ptr + 1];
        buffer2   = NTC[ptr] - T_Value;
        F_Value   = buffer2 * 10.0 / buffer1;
        Tem_Value = ptr * 10 + F_Value - 400;
        return (Tem_Value);
    }
}

/* 平滑滤波取平均值*/
short NTC_Caculate(unsigned short T_Data)
{
    unsigned int NTC_Ohm;  // NTC电阻
    short        Return_Data = 0;
    NTC_Ohm                  = 330000.0 * T_Data / (25.0 * 4095 - 33.0 * T_Data);
    if (NTC_Ohm >= 286228)
        NTC_Ohm = 286228;
    else {
        if (NTC_Ohm <= 543)
            NTC_Ohm = 543;
    }
    Return_Data = LinearInsert(NTC_Ohm);
    return Return_Data;
}

/* 平滑滤波取平均值*/
unsigned short exch(unsigned short *p, unsigned char Num)  // 平滑滤波取平均值
{
    unsigned char  i                = 0;
    unsigned char  j                = 0;
    unsigned short Exch_Buffer[255] = {0};
    unsigned short Buffer_Data      = 0;
    unsigned int   Return_Data      = 0;
    for (i = 0; i < Num; i++) {
        Exch_Buffer[i] = *p;
        p++;
    }
    for (i = 0; i < Num; i++)  // 从小到大 冒泡排序
    {
        for (j = 0; j < (Num - i - 1); j++) {
            if (Exch_Buffer[j] > Exch_Buffer[j + 1]) {
                Buffer_Data        = Exch_Buffer[j];
                Exch_Buffer[j]     = Exch_Buffer[j + 1];
                Exch_Buffer[j + 1] = Buffer_Data;
            }
        }
    }
    for (i = 2; i < (Num - 2); i++) {
        Return_Data += Exch_Buffer[i];
    }
    return (unsigned short)(Return_Data / (Num - 4));
}

#if 0
unsigned short averge_adc_data(unsigned int dma_buf[ADC_DMA_BUF_NUM][ADC_CHANNEL_NUM] , unsigned char channel_num , unsigned char len)
{
  unsigned char i=0;
	unsigned char j=0;
	unsigned short Exch_Buffer[ADC_DMA_BUF_NUM];
	unsigned short Buffer_Data=0;
	unsigned int Return_Data=0;

  for(i=0 ; i<ADC_DMA_BUF_NUM ; i++)
	{
		Exch_Buffer[i] = dma_buf[i][channel_num];
	}
  
  for(i=0;i<len;i++)                              //从小到大 冒泡排序
	{
		for(j=0;j<(len-i-1);j++)
		{
			if(Exch_Buffer[j]>Exch_Buffer[j+1])
			{
				Buffer_Data=Exch_Buffer[j];
				Exch_Buffer[j]=Exch_Buffer[j+1];
				Exch_Buffer[j+1]=Buffer_Data;
			}
		
		}
	}
  
  for(i=2;i<(len-2);i++)
	{
		Return_Data+=Exch_Buffer[i];
	}
	return (unsigned short)(Return_Data/(len-4));
  
  
}
#endif

unsigned short averge_adc_data(unsigned int dma_buf[ADC_DMA_BUF_NUM][ADC_CHANNEL_NUM], unsigned char channel_num, unsigned char len)
{
    unsigned char i = 0;

    unsigned int   sum = 0;
    unsigned short max = dma_buf[0][channel_num];
    unsigned short min = dma_buf[0][channel_num];

    for (i = 0; i < ADC_DMA_BUF_NUM; i++) {

        sum += dma_buf[i][channel_num];
        if (dma_buf[i][channel_num] < min)
            min = dma_buf[i][channel_num];

        if (dma_buf[i][channel_num] > max)
            max = dma_buf[i][channel_num];
    }

    return ((sum - min - max) / (ADC_DMA_BUF_NUM - 2));
}

/* 取最大值*/

unsigned short adc_data_max(unsigned int adc_buf[ADC_DMA_BUF_NUM][ADC_CHANNEL_NUM], unsigned char channel, unsigned char Num)
{
    unsigned char  i           = 0;
    unsigned short Return_Data = adc_buf[0][channel];
    for (i = 0; i < Num; i++) {
        if (Return_Data < adc_buf[i][channel])
            Return_Data = adc_buf[i][channel];
    }
    return Return_Data;
}

void get_sensor_data(void)
{
    float                   sensor_data = 0.0;
    volatile unsigned short tmp         = 0;
    memory_copy((void *)adc_app_buf, (void *)adc_dma_buf, sizeof(adc_app_buf));
    operate_parm.T1_Value = NTC_Caculate(averge_adc_data((void *)adc_app_buf, 0, ADC_DMA_BUF_NUM)) / 5;
    operate_parm.T2_Value = NTC_Caculate(averge_adc_data((void *)adc_app_buf, 1, ADC_DMA_BUF_NUM)) / 5;
    // operate_parm.T3_Value = NTC_Caculate(averge_adc_data((void*)adc_app_buf , 2 , ADC_DMA_BUF_NUM)) / 5;
    operate_parm.T4_Value = NTC_Caculate(averge_adc_data((void *)adc_app_buf, 3, ADC_DMA_BUF_NUM)) / 5;
    operate_parm.PD1      = adc_data_max((void *)adc_app_buf, 4, ADC_DMA_BUF_NUM) * ADC_Baseline / ADC_Accuracy / 20;
    // operate_parm.PD2 = adc_data_max((void*)adc_app_buf , 5 , ADC_DMA_BUF_NUM) * ADC_Baseline / ADC_Accuracy / 20;
    // operate_parm.PD3 = adc_data_max((void*)adc_app_buf , 6 , ADC_DMA_BUF_NUM) * ADC_Baseline / ADC_Accuracy / 20;

    tmp                   = averge_adc_data((void *)adc_app_buf, 7, ADC_DMA_BUF_NUM);
    sensor_data           = (averge_adc_data((void *)adc_app_buf, 7, ADC_DMA_BUF_NUM) * 218.75 / ADC_Accuracy - 66.875) * 2;
    operate_parm.ET_Value = sensor_data;

    operate_parm.EH_Value = averge_adc_data((void *)adc_app_buf, 8, ADC_DMA_BUF_NUM) * 125 / ADC_Accuracy - 12.5;
}
